package com.example.myandroidactualcombat;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.Adapter.BookAdapter;
import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.ChooseBook;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class BooksActivity extends AppCompatActivity{

    private Button bu_Collection;
    private TextView tv_title;
    private ListView lv;
    ArrayList<Book> dataList;
    List<Book>bookList;
    BookAdapter adapter;
    String username;
    int userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.books_main2);
        addBook();//添加图书
        initView();//初始化控件
        getUser();//获取用户
        addListener();//添加监听（点击）
        addListener1();//添加监听（长按）
    }

    private void addListener1() {
        //listview长按事件
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                //bookid为用户id,相当于book表里面的id主键（张三,张四,张五）
                int bookid = bookList.get(i).getId();
                //分别是ChooseBook里面的用户id,图书id(userid)
                ChooseBook chooseBook = new ChooseBook(bookid,userid);
                //创建一个ChooseBook集合，只要符合where()里面的条件就储存在Choosebook集合里
                //每本书有两个id,分别是bookid(书名)和userid(用户)，把长按的这本书的两个id拿去和ChooseBook表去对比
                //假如书本的两个id和收藏表的重复了，就代表某个用户重复收藏了该书，然后把重复的书放进ChooseBook集合里
                List<ChooseBook> chooseBookList = LitePal.where("userid=? and bookid=?",""+userid,""+bookid).find(ChooseBook.class);
                //所以要没重复的书才能被保存在ChooseBook表里
                if (chooseBookList.size()==0){
                    Log.i("TG", "onItemLongClfjf"+chooseBookList.size());
                    chooseBook.save();
                    Toast.makeText(BooksActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(BooksActivity.this,"此书已收藏",Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
    }

    private void addListener() {
        //listview的点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //跳转事件，参数分别为上下文
                Intent intent=new Intent(BooksActivity.this,ReadActivity.class);
                //如果要传递某些String类型数据给Activity2，则会执行intent.putExtra(String str, String Key),夹带内容为：booklist里面的bookname(书名)
                intent.putExtra("bookname",bookList.get(i).getBookname());
                //启动改intent对象，实现跳转
                startActivity(intent);
            }
        });
    }

    private void getUser() {
        //通过getDefaultSharedPreferences方法得到SharedPreferences的对象
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
        //分别调用getString,getint方法来获取前面储存的书名和序号
        username=pref.getString("username","无值");//这键值对的意思是，如果找不到相应的值就用后面的值替代
        userid=pref.getInt("userid",9);//假如userid没有值，将用9替代
        tv_title.setText("你好，"+userid+"号用户"+username);
    }

    private void addBook() {
       Book book0=new Book(0,"三体",R.drawable.z);//在litepal数据库里新增一条数据
       book0.save();//保存
       Book book1=new Book(1,"狼王梦",R.drawable.z);
       book1.save();
       Book book2=new Book(2,"第七条猎狗",R.drawable.z);
       book2.save();
       Book book3=new Book(3,"鸟奴",R.drawable.z);
       book3.save();

    }
    private void initView() {
        //初始化控件
        bu_Collection = (Button) findViewById(R.id.bu_Collection);
        tv_title = (TextView) findViewById(R.id.tv_title);
        lv = (ListView) findViewById(R.id.lv);
        //查询数据库所有内容
        bookList=LitePal.findAll(Book.class);
        //dataList里面的数据赋值到集合里
        dataList=new ArrayList<>();
        //booklist里面有多少条目，都添加到datalist集合里面，添加的内容是booklist里面的Bookname(书名);
        for (int i=0;i<bookList.size();i++){
            dataList.add(bookList.get(i));
            Log.i("TAG", "idadaad"+dataList.get(i).getImageid());
        }
        //适配器显示datalist(数据源)里面的内容，simple_list_item_1为子项布局
        //adapter = new ArrayAdapter<>(BooksActivity.this,android.R.layout.simple_list_item_1,dataList);
        adapter =new BookAdapter(BooksActivity.this,R.layout.book_item,bookList);
        //把数据添加到listview里面
        lv.setAdapter(adapter);
        //点击跳转
        bu_Collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(BooksActivity.this,ChooseActivity.class));
            }
        });
    }
}
